<template>
    <view :style="{height:screenHeight + 'px',background:'#f6f6f6',paddingTop:'30rpx'}">
      <view>
        <shopping-goods
            @del="delGoods"
            v-for="(item,index) in addstocklist" :key="index"
            :index="index"
            :goods="item" :type="type" :photo="photo"
            :updateNum="item.isGift !== '1'"
            @selectGoods="setSelectGoods"
            :currentIndex.sync="currentIndex"
            showIndex
        ></shopping-goods>
        
      </view>
      <!-- 上报 -->
      <stock :visible="false" :type="type" :news="news" :photo="photo" :addstocklist="addstocklist" :back="back"/>
  <!-- 遮罩层 -->
      <!-- <view class="mask" @touchmove.stop.prevent="" @tap="close" @close="close" v-if="mask">
        <view></view>
      </view> -->
      <uni-popup   maskBackgroundColor="rgba(69,64,68,.6)" ref="popup" type="bottom"  :animation="true">
           <!-- @addGoods="addGoods" -->
        <select-goods
          @addstock="addstock"
          @subPromotionList="subPromotionList"
          :customer-id="customerId"
          :goods.sync="currentGoods"
          @close="close" :type="type" :news="news" :photo="photo"
          :currentGiftList.sync="currentGiftList"
          :currentPromotionId.sync="currentPromotionId"
        ></select-goods>
      </uni-popup>
    </view>
  </template>
  
  <script>
  import shoppingGoods from "../components/shoppingGoods";
  import {getScreen} from "../../service/auth";
  import stock from "../../components/stock";
  import selectGoods from "../../components/selectGoods";
  export default {
    name: "index",
    components:{shoppingGoods, stock, selectGoods},
    data(){
      return{
        addstocklist:[],
        screenHeight: 0,
        customerId: '',
        currentGoods: {},
        mask: false,
        currentPromotionId: -1,
        currentGiftList:[],
        currentIndex: -1,
        type:'',
        news:'',
        photo:'',
        back:'1',
      }
    },
    onShow(){
  
    },
    onLoad(option){
        this.type=option.type
        this.news=option.news
        this.photo=option.photo
        console.log('4545454545',option)
    },
    created() {
      const app = getApp()
      this.addstocklist = app.globalData.addstocklist
      this.customerId = app.globalData.selectedPosition.id
      const {height} = getScreen()
      this.screenHeight = height
    },
    methods:{
      setSelectGoods(val){
        this.currentGoods = val
        this.currentPromotionId = val.currentPromotionId
        if (val.promotionGroupKey){
          this.currentGiftList = this.addstocklist.filter(item=>item.promotionGroupKey === val.promotionGroupKey && item.isGift === '1')
        }
        this.$refs.popup.open()
        this.mask = true
      },
      addstock(val){
        const index = this.addstocklist.findIndex(item=>item.goodsId === val.goodsId && !item.promotionGroupKey && !item.giftId)
        this.addstocklist.splice(index,1,val)
        this.close()
      },
      subPromotionList(promotionList){
        this.delGoods(this.currentIndex)
        this.addstocklist.splice(this.currentIndex,promotionList.length,...promotionList)
      },
      delGoods(index){
        //   console.log('090000000000099',index)
        // if (this.addstocklist[index].isPromotion){
        //   if (this.addstocklist[index].isGift === '1'){
        //     const promotionEmptyBoxKey = this.addstocklist[index].promotionEmptyBoxKey
        //     this.addstocklist.splice(index, 1)
        //     if (promotionEmptyBoxKey){
        //       const index1 = this.addstocklist.findIndex(item=>item.promotionEmptyBoxKey === promotionEmptyBoxKey)
        //       this.addstocklist.splice(index1, 1)
        //     }
        //     return
        //   }
        //   const promotionGroupKey = this.addstocklist[index].promotionGroupKey
        //   console.log(promotionGroupKey,promotionGroupKey,'66666666')
        //   const promotionGoodsList = this.addstocklist.filter(item=>item.promotionGroupKey === promotionGroupKey)
        //   for (let i in promotionGoodsList){
        //     this.addstocklist.splice(this.addstocklist.indexOf(promotionGoodsList[i]),1)
        //   }
        //   return
        // }
        this.addstocklist.splice(index, 1)
      },
      close(){
        this.$refs.popup.close()
        this.mask = false
      },
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .body{
    background: #f6f6f6;
    height: 100rpx;
  }
  </style>
  